package com.biodiv.community.base;

import com.biodiv.community.constant.CommonConstant;
import com.biodiv.community.enums.ErrorEnum;
import lombok.Data;
import org.slf4j.MDC;

import static com.biodiv.community.constant.CommonConstant.TRACE_ID;

/**
 * @author tianquan
 */
@Data
public class ResponseEntity<T> {

    private int code;

    private String message;

    private T data;

    private String traceId;

    public ResponseEntity() {
        traceId = MDC.get(TRACE_ID);
    }
    
    public static <T> ResponseEntity<T> success() {
        ResponseEntity<T> response = new ResponseEntity<>();
        response.setCode(ErrorEnum.SUCCESS.getCode());
        response.setMessage(ErrorEnum.SUCCESS.getMessage());
        return response;
    }
    
    public static <T> ResponseEntity<T> success(T data) {
        ResponseEntity<T> response = success();
        response.setData(data);
        return response;
    }
    
    public static <T> ResponseEntity<T> error(int code, String message) {
        ResponseEntity<T> response = new ResponseEntity<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }
    
    public static <T> ResponseEntity<T> error(ErrorEnum errorEnum) {
        return error(errorEnum.getCode(), errorEnum.getMessage());
    }

}
